package com.setting.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.setting.entity.JsonUsed;
import com.utils.JsonResult;
import com.entity.CalculateUse;
import com.entity.Meter;
import com.entity.Org;
import com.entity.Report_electri_Power;
import com.interfaces.CalculateUseDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.Report_electri_PowerDao;
import com.interfaces.SysdicDao;
import com.mysql.fabric.xmlrpc.base.Array;
import com.report.entity.JsonTaskRecordReport;

/**
 * @Title: UsedService.java
 * @Package com.setting.service
 * @author 孙梦楠
 * @date 2017年10月17日 下午5:13:55
 * @version V1.0.0
 */
@Service
public class UsedService {

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private OrgDao orgdao;

	@Autowired
	private CalculateUseDao calculateusedao;

	@Autowired
	private Report_electri_PowerDao reportelectripowerdao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private CustomerMeterDao customermeterdao;

	// 一段时间内某公司某小区的总用户数和总用电量

	@Transactional
	public JsonResult<List<JsonUsed>> areaUsedAndCustomerCount(String org, String type, String luaguage, String code,
			String begintime, String endtime) {

		Integer CustomercountCompany = 0;
		Double UsedCompany1 = 0.0;
		Double UsedCompany2 = 0.0;
		Double UsedCompany3 = 0.0;
		Double UsedCompany4 = 0.0;
		Double UsedCompany5 = 0.0;
		Integer CustomercountArea = 0;
		Double UsedArea1 = 0.0;
		Double UsedArea2 = 0.0;
		Double UsedArea3 = 0.0;
		Double UsedArea4 = 0.0;
		Double UsedArea5 = 0.0;

		List<JsonUsed> jsonUseds = new ArrayList<JsonUsed>();

		if (orgdao.findByOrgvalue(org).getOrgfather().equals("0")) {// 是公司
			List<Org> orgs = orgdao.findByOrgfather(org);
			JsonUsed jsonUsed2 = new JsonUsed();
			// 遍历小区
			for (Org org2 : orgs) {
				JsonUsed jsonUsed = calcuateCountAndUsed(org2.getOrgvalue(), type, luaguage, CustomercountArea,
						UsedArea1, UsedArea2, UsedArea3, UsedArea4, UsedArea5, code, begintime, endtime);
				CustomercountCompany += jsonUsed.getCustomercount();
				UsedCompany1 += jsonUsed.getUsed1();
				UsedCompany2 += jsonUsed.getUsed2();
				UsedCompany3 += jsonUsed.getUsed3();
				UsedCompany4 += jsonUsed.getUsed4();
				UsedCompany5 += jsonUsed.getUsed5();
				jsonUseds.add(jsonUsed);
			}
			jsonUsed2.setCompany(sysdicservice.finddisplay("0001", org, luaguage));
			jsonUsed2.setArea("全部小区");
			jsonUsed2.setCustomercount(CustomercountCompany);
			double f1 = new BigDecimal(UsedCompany1).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			double f2 = new BigDecimal(UsedCompany2).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			double f3 = new BigDecimal(UsedCompany3).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			double f4 = new BigDecimal(UsedCompany4).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			double f5 = new BigDecimal(UsedCompany5).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			jsonUsed2.setUsed1(f1);
			jsonUsed2.setUsed2(f2);
			jsonUsed2.setUsed3(f3);
			jsonUsed2.setUsed4(f4);
			jsonUsed2.setUsed5(f5);
			jsonUseds.add(jsonUsed2);

		} else {// 是小区

			JsonUsed jsonUsed = calcuateCountAndUsed(org, type, luaguage, CustomercountArea, UsedArea1, UsedArea2,
					UsedArea3, UsedArea4, UsedArea5, code, begintime, endtime);
			jsonUseds.add(jsonUsed);
		}

		return new JsonResult<List<JsonUsed>>(751, "用户数和用量", jsonUseds);
	}

	private JsonUsed calcuateCountAndUsed(String org, String type, String luaguage, Integer CustomercountArea,
			Double UsedArea1, Double UsedArea2, Double UsedArea3, Double UsedArea4, Double UsedArea5, String code,
			String begintime, String endtime) {

		JsonUsed jsonUsed = new JsonUsed();
		// 电表
		List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatus(Integer.valueOf(org), type, "3");

		List<Report_electri_Power> calculateUses = reportelectripowerdao
				.findTaskrecorditemByMeter2(Integer.valueOf(org), code, begintime, endtime, type);
		for (Report_electri_Power calculateUse : calculateUses) {
			UsedArea1 += Double.valueOf(calculateUse.getValue1());
			UsedArea2 += Double.valueOf(calculateUse.getValue2());
			UsedArea3 += Double.valueOf(calculateUse.getValue3());
			UsedArea4 += Double.valueOf(calculateUse.getValue4());
			UsedArea5 += Double.valueOf(calculateUse.getValue5());
		}
		CustomercountArea = meters.size();

		jsonUsed.setCompany(sysdicservice.finddisplay("0001", orgdao.findByOrgvalue(org).getOrgfather(), luaguage));
		jsonUsed.setArea(sysdicservice.finddisplay("0001", org, luaguage));
		jsonUsed.setCustomercount(CustomercountArea);
		// 保留一位小数
		BigDecimal bg1 = new BigDecimal(UsedArea1);
		double f1 = bg1.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		BigDecimal bg2 = new BigDecimal(UsedArea2);
		double f2 = bg2.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		BigDecimal bg3 = new BigDecimal(UsedArea3);
		double f3 = bg3.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		BigDecimal bg4 = new BigDecimal(UsedArea4);
		double f4 = bg4.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		BigDecimal bg5 = new BigDecimal(UsedArea5);
		double f5 = bg5.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		jsonUsed.setUsed1(f1);// 总
		jsonUsed.setUsed2(f2);// 尖
		jsonUsed.setUsed3(f3);// 峰
		jsonUsed.setUsed4(f4);// 平
		jsonUsed.setUsed5(f5);// 谷
		return jsonUsed;

	}

	// 一段时间内的总成功率
	public JsonResult<List<JsonUsed>> successrate(String org, String type, String luaguage, String code, String time) {

		List<JsonUsed> jsonUseds = new ArrayList<JsonUsed>();

		if (orgdao.findByOrgvalue(org).getOrgfather().equals("0")) {// 是公司
			List<Org> orgs = orgdao.findByOrgfather(org);
			// JsonUsed jsonUsed2 = new JsonUsed();
			// 遍历小区
			for (Org org2 : orgs) {
				JsonUsed jsonUsed = calculateRate(org2.getOrgvalue(), type, luaguage, code, time);
				jsonUseds.add(jsonUsed);
			}
		} else {
			JsonUsed jsonUsed = calculateRate(org, type, luaguage, code, time);
			jsonUseds.add(jsonUsed);
		}
		return new JsonResult<List<JsonUsed>>(752, "成功率", jsonUseds);
	}

	// 计算抄收成功率
	private JsonUsed calculateRate(String org, String type, String luaguage, String code, String time) {
		JsonUsed jsonUsed = new JsonUsed();
		String successrate = null;
		List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatus(Integer.valueOf(org), type, "2");
		if (meters.size() != 0) {
			if (time == null) {
				Date d = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				time = sdf.format(d);
			}
			Integer count1 = reportelectripowerdao.customercountByMeter(time, Integer.valueOf(org), type, code);
			Integer count2 = meters.size();
			Double success = (double) count1 / (double) count2;
			DecimalFormat df = new DecimalFormat("0.0000");
			successrate = Double.valueOf(df.format(success)) * 100 + "%";
		}
		jsonUsed.setCompany(sysdicservice.finddisplay("0001", orgdao.findByOrgvalue(org).getOrgfather(), luaguage));
		jsonUsed.setArea(sysdicservice.finddisplay("0001", org, luaguage));
		jsonUsed.setSuccessrate(successrate);
		return jsonUsed;
	}

	// 通过表计查总报表 曲线?1
	@Transactional
	public JsonResult<Object[][]> recordreportLine(Integer customerid, String code, String time1, String time2,
			String status2) {

		String orgvalue = customerid.toString();
		String begintime = null;
		String endtime = null;
		if (time1 == null) {
			begintime = null;
		} else {
			begintime = time1 + " 00:00:00";
		}
		if (time2 == null) {
			endtime = null;
		} else {
			endtime = time2 + " 00:00:00";

		}
		if (orgdao.findByOrgvalue(orgvalue).getOrgfather().equals("0")) {// 公司
			List<Report_electri_Power> list = reportelectripowerdao.totelcount2(code, begintime, endtime,
					Integer.valueOf(orgvalue));
			Object[][] obj = new Object[list.size()][2];
			int i = 0;
			int j = 0;
			// TIME字段
			for (Report_electri_Power report_electri_Power : list) {
				String time = report_electri_Power.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = formatter.parse(time);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				obj[i][0] = date.getTime() + 28800000;
				i++;
			}
			// 总量字段
			List<Double> values = reportelectripowerdao.totelvaluecompany1(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j][1] = f1;
				j++;
			}

			return new JsonResult<Object[][]>(681, "所有报表_曲线", obj);

		} else {
			List<Report_electri_Power> list = reportelectripowerdao.totelcount(code, begintime, endtime,
					Integer.valueOf(orgvalue));
			Object[][] obj = new Object[list.size()][2];
			int i = 0;
			int j = 0;
			for (Report_electri_Power report_electri_Power : list) {
				String time = report_electri_Power.getTime();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = formatter.parse(time);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				obj[i][0] = date.getTime() + 28800000;
				i++;
			}
			List<Double> values = reportelectripowerdao.totelvaluearea1(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j][1] = f1;
				j++;
			}

			return new JsonResult<Object[][]>(681, "所有报表_曲线", obj);
		}

	}

	// 表格
	@Transactional
	public JsonResult<List<JsonTaskRecordReport>> recordreportTable(Integer customerid, String code, String time1,
			String time2, String status2, String type, String languagecookie) {

		String orgvalue = customerid.toString();
		String begintime = null;
		String endtime = null;
		if (time1 == null) {
			begintime = null;
		} else {
			begintime = time1 + " 00:00:00";
		}
		if (time2 == null) {
			endtime = null;
		} else {
			endtime = time2 + " 00:00:00";

		}
		List<JsonTaskRecordReport> totellist = new ArrayList<JsonTaskRecordReport>();
		if (orgdao.findByOrgvalue(orgvalue).getOrgfather().equals("0")) {// 公司
			List<Org> orgs = orgdao.findByOrgfather(orgvalue);
			Integer metercount = 0;
			for (Org org : orgs) {
				List<Meter> meters = meterdao.findchildmeterByMeterorgAndTypeAndMeterstatus(Integer.valueOf(org.getOrgvalue()),
						type, "3");
				metercount += meters.size();
			}
			List<Report_electri_Power> list = reportelectripowerdao.totelcount2(code, begintime, endtime,
					Integer.valueOf(orgvalue));
			Object[][] obj = new Object[list.size()][9];
			int i = 0;
			int j1 = 0;
			int j2 = 0;
			int j3 = 0;
			int j4 = 0;
			int j5 = 0;
			int j6 = 0;
			// TIME字段
			for (Report_electri_Power report_electri_Power : list) {
				obj[i][0] = report_electri_Power.getTime();
				obj[i][1] = report_electri_Power.getName();
				obj[i][2] = report_electri_Power.getUpdatetime();
				i++;
			}
			// 总量字段
			List<Double> values1 = reportelectripowerdao.totelvaluecompany1(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values1) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j1][3] = f1;
				j1++;
			}
			List<Double> values2 = reportelectripowerdao.totelvaluecompany2(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values2) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j2][4] = f1;
				j2++;
			}
			List<Double> values3 = reportelectripowerdao.totelvaluecompany3(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values3) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j3][5] = f1;
				j3++;
			}
			List<Double> values4 = reportelectripowerdao.totelvaluecompany4(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values4) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j4][6] = f1;
				j4++;
			}
			List<Double> values5 = reportelectripowerdao.totelvaluecompany5(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values5) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j5][7] = f1;
				j5++;
			}
			List<Double> values6 = reportelectripowerdao.totelvaluecompany6(code, time1, time2,
					Integer.valueOf(orgvalue));
			for (Double double1 : values6) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j6][8] = f1;
				j6++;
			}
			for (Object[] objects : obj) {
				JsonTaskRecordReport electri_Power = new JsonTaskRecordReport();
				electri_Power.setTime(objects[0].toString());
				electri_Power.setValue1(objects[3].toString());
				electri_Power.setValue2(objects[4].toString());
				electri_Power.setValue3(objects[5].toString());
				electri_Power.setValue4(objects[6].toString());
				electri_Power.setValue5(objects[7].toString());
				electri_Power.setValue6(objects[8].toString());
				electri_Power.setName(objects[1].toString());
				electri_Power.setTerminaladdress(metercount + "");
				electri_Power.setMeteraddress(sysdicservice.finddisplay("0001", orgvalue, languagecookie));
				electri_Power.setUpdatetime(objects[2].toString());
				totellist.add(electri_Power);
			}

			return new JsonResult<List<JsonTaskRecordReport>>(682, "所有报表_表格", totellist);

		} else {
			List<Meter> meters = meterdao.findchildmeterByMeterorgAndTypeAndMeterstatus(customerid, type, "3");
			List<Report_electri_Power> list = reportelectripowerdao.totelcount(code, begintime, endtime,
					Integer.valueOf(orgvalue));
			Object[][] obj = new Object[list.size()][9];
			int i = 0;
			int j1 = 0;
			int j2 = 0;
			int j3 = 0;
			int j4 = 0;
			int j5 = 0;
			int j6 = 0;
			for (Report_electri_Power report_electri_Power : list) {
				obj[i][0] = report_electri_Power.getTime();
				obj[i][1] = report_electri_Power.getName();
				obj[i][2] = report_electri_Power.getUpdatetime();
				i++;
			}
			List<Double> values1 = reportelectripowerdao.totelvaluearea1(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values1) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j1][3] = f1;
				j1++;
			}
			List<Double> values2 = reportelectripowerdao.totelvaluearea2(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values2) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j2][4] = f1;
				j2++;
			}
			List<Double> values3 = reportelectripowerdao.totelvaluearea3(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values3) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j3][5] = f1;
				j3++;
			}
			List<Double> values4 = reportelectripowerdao.totelvaluearea4(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values4) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j4][6] = f1;
				j4++;
			}
			List<Double> values5 = reportelectripowerdao.totelvaluearea5(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values5) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j5][7] = f1;
				j5++;
			}
			List<Double> values6 = reportelectripowerdao.totelvaluearea6(code, time1, time2, Integer.valueOf(orgvalue));
			for (Double double1 : values6) {
				BigDecimal bg1 = new BigDecimal(double1.doubleValue());
				double f1 = bg1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				obj[j6][8] = f1;
				j6++;
			}

			for (Object[] objects : obj) {
				JsonTaskRecordReport electri_Power = new JsonTaskRecordReport();
				electri_Power.setTime(objects[0].toString());
				electri_Power.setValue1(objects[3].toString());
				electri_Power.setValue2(objects[4].toString());
				electri_Power.setValue3(objects[5].toString());
				electri_Power.setValue4(objects[6].toString());
				electri_Power.setValue5(objects[7].toString());
				electri_Power.setValue6(objects[8].toString());
				electri_Power.setName(objects[1].toString());
				electri_Power.setTerminaladdress(meters.size() + "");
				electri_Power.setMeteraddress(sysdicservice.finddisplay("0001", orgvalue, languagecookie));
				electri_Power.setUpdatetime(objects[2].toString());
				totellist.add(electri_Power);
			}

			return new JsonResult<List<JsonTaskRecordReport>>(682, "所有报表_表格", totellist);
		}

	}

}
